package com.artPreter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.artPreter.mapper.ArtistMapper;
import com.artPreter.pojo.Artist;
import com.artPreter.pojo.ArtistTemp;
import com.artPreter.mapper.ArtistTempMapper;
import com.artPreter.service.IArtistTempService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 艺术家模板内容表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-23
 */
@Service
public class ArtistTempServiceImpl extends ServiceImpl<ArtistTempMapper, ArtistTemp> implements IArtistTempService {

    @Autowired
    private ArtistMapper artistMapper;

    /**
     * 获取艺术家模板，如果没有模板则添加一个新模板
     * @param
     * @return
     */
    @Override
    public ArtistTemp getArtistTemp(ArtistTemp artistTemp) {
        LambdaQueryWrapper<ArtistTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ArtistTemp::getArtistId,artistTemp.getArtistId());
        List<ArtistTemp> list = this.baseMapper.selectList(lambdaQueryWrapper);
        if (list.size() == 0){
            ArtistTemp result = new ArtistTemp();
            String uuid = java.util.UUID.randomUUID().toString();
            result.setContentId(uuid);
            LambdaQueryWrapper<Artist> artistLambdaQueryWrapper = new LambdaQueryWrapper<>();
            artistLambdaQueryWrapper.eq(Artist::getArtistId,artistTemp.getArtistId());
            List<Artist> artistList = artistMapper.selectList(artistLambdaQueryWrapper);
            if (artistList.size() > 0){
                result.setName(artistList.get(0).getName()+"模板");
                result.setArtistId(artistList.get(0).getArtistId());
            }
            this.baseMapper.insert(result);
            return result;
        }else{
            return list.get(0);
        }

    }

    @Override
    public void sveArtistTemp(ArtistTemp artistTemp) {
        this.baseMapper.updateById(artistTemp);
    }
}
